local skel = fk.CreateSkill {
  name = "lb__shouxun",
}

Fk:loadTranslationTable {
  ["lb__shouxun"] = "授勋",
  [":lb__shouxun"] = "出牌阶段开始时，若你已受伤，你可以展示牌堆顶的两张牌，获得其中一张牌并记录另一张的类型；结束阶段，若你本回合使用了至少两张记录类型的牌，你摸两张牌并交出〖授勋〗。每轮开始时，你将〖授勋〗交给银枝。",

  ["#lb__shouxun_end"] = "授勋",
  ["#lb__shouxun_start"] = "授勋",
  ["@lb__shouxun-turn"] = "授勋",
  ["#lb__shouxun"] = "授勋：你获得其中一张牌并记录另一张的类型",
  ["#lb__shouxun-p"] = "授勋：将〖授勋〗交给一名角色",

  ["$lb__shouxun1"] = "荣光在上。",
  ["$lb__shouxun2"] = "再次见到那道光芒之前……",
  ["$lb__shouxun3"] = "彷徨的人啊，你的目光正看向何方？",
  ["$lb__shouxun4"] = "我们定会于宇宙某处偶然相逢。",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  audio_index = { 1, 2 },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Play and player:isWounded()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = room:getNCards(2)
    room:moveCards({
      ids = ids,
      toArea = Card.Processing,
      moveReason = fk.ReasonJustMove,
      skillName = skel.name,
      proposer = player,
    })
    local card = room:askToChooseCardsAndChoice(player, {
      cards = ids,
      skill_name = skel.name,
      prompt = "#lb__shouxun",
    })
    for _, c in ipairs(ids) do
      if c == card[1] then
        room:obtainCard(player, c, true, fk.ReasonJustMove, player, "lb__shouxun")
      else
        room:setPlayerMark(player, "@lb__shouxun-turn", Fk:getCardById(c):getTypeString())
        if room:getCardArea(c) == Card.Processing then
          room:moveCardTo(c, Card.DrawPile, nil, fk.ReasonJustMove, "lb__shouxun", nil, true, player)
        end
      end
    end
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  audio_index = { 3, 4 },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and
        player.phase == Player.Finish and player:getMark("@lb__shouxun-turn") ~= 0
        and (#player.room.logic:getEventsOfScope(GameEvent.UseCard, 2, function(e)
          local use = e.data
          return use.from == player and use.card:getTypeString() == player:getMark("@lb__shouxun-turn")
        end, Player.HistoryTurn) > 1)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, skel.name)
    local t = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#lb__shouxun-p",
      skill_name = skel.name,
      cancelable = false,
    })
    if #t > 0 then
      room:handleAddLoseSkills(player, "-lb__shouxun")
      room:handleAddLoseSkills(t[1], "lb__shouxun")
    end
  end,
})

skel:addEffect(fk.RoundStart, {
  anim_type = "support",
  audio_index = { 3, 4 },
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and not table.contains({ player.general, player.deputyGeneral }, "lb__yinzhi")
  end,
  on_cost = function(self, event, target, player, data)
    local p = table.find(player.room:getOtherPlayers(player), function(t)
      return table.contains({ t.general, t.deputyGeneral }, "lb__yinzhi")
    end)
    event:setCostData(self, { tos = p })
    return p
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:handleAddLoseSkills(player, "-lb__shouxun")
    room:handleAddLoseSkills(event:getCostData(self).tos, "lb__shouxun")
  end,
})

return skel
